package com.xzw.cameratest.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.business.entity.ChannelInfo;
import com.android.business.entity.DataInfo;
import com.android.business.entity.GroupInfo;
import com.xzw.cameratest.R;
import com.xzw.cameratest.bean.Node;

import java.util.List;

/**
 * @descripe:
 * @author:111144
 * @date:2020/4/26
 */
public class OrganizationTreeAdapter extends RecyclerView.Adapter<OrganizationTreeAdapter.ViewHolder> {
    private Context mContext;
    private List<Node> mVisibleNodes;
    private OnItemClickListener onItemClickListener;

    public OrganizationTreeAdapter(Context context, List<Node> nodes){
        this.mContext = context;
        this.mVisibleNodes = nodes;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_organzation_tree_list,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        Node node = mVisibleNodes.get(i);
        DataInfo data = node.getData();
        if (data instanceof GroupInfo){
            viewHolder.tvItemName.setText(((GroupInfo) data).getGroupName());
            viewHolder.ivLeft.setImageResource(R.mipmap.icon_grouptree_organize_online2);

        }else if (data instanceof ChannelInfo){
            viewHolder.tvItemName.setText(((ChannelInfo)data).getName());
            boolean isOnline = ((ChannelInfo) data).getState() == ChannelInfo.ChannelState.Online;
            viewHolder.ivLeft.setImageResource(isOnline? R.mipmap.icon_grouptree_channel_normal_online2:R.mipmap.icon_grouptree_channel_normal_offline);

        }

        viewHolder.tvItemDescription.setText(node.getParentNodeName());
        viewHolder.llItem.setPadding(node.getLevel() * 30,0,0,0);
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null){
                    onItemClickListener.onClick(i);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mVisibleNodes == null ? 0 : mVisibleNodes.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private LinearLayout llItem;
        private TextView tvItemName;
        private TextView tvItemDescription;
        private ImageView ivLeft;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivLeft = itemView.findViewById(R.id.iv_left);
            llItem = itemView.findViewById(R.id.ll_item);
            tvItemName = itemView.findViewById(R.id.tv_item_name);
            tvItemDescription = itemView.findViewById(R.id.tv_item_description);
        }
    }

    public interface OnItemClickListener{
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }


}
